<?php
/**
 * 访问数据库新闻类
 */

class News extends CI_Controller
{
	/**
	 * 初始化构造方法
	 */
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		echo "我是index" . PHP_EOL;
	}

	public function news_count()
	{
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'news'
		];
		$count = $this->news_model->get_conunt($cont);
		$content = [
			'code' => 200,
			'count' => $count
		];
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}


	/**
	 * 读取最新文章
	 */
	public function read_new_news()
	{
		$rows = $this->input->post('limit'); //获取文章显示数量
		$page = $this->input->post('page'); //获取文章显示页数

		$rows = (int)$rows; // 全部转为整型
		$page = (int)$page; // 全部转为整型

		$this->load->model('news_model');
		$isok = $this->news_model->read_new_news_mysql(($page - 1) * $rows, $rows);
		if ($isok == null) {
			$content = [
				'code' => 400,
				'msg' => "获取失败！"
			];
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
			return;
		}
		$count = $this->news_model->news_count("news");
		$news_arr = array();
		$news_arr['code'] = 0;
		$news_arr['msg'] = "";
		$news_arr['count'] = (int)$count;
		$news_arr['data'] = $isok;
		echo json_encode($news_arr, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}


	/**
	 * get传入id，返回指定id的文章
	 */
	public function id_news()
	{
		$id = $this->input->get('id'); //post文章标题 根据
		$this->load->model('news_model');
		$data = $this->news_model->id_news_mysql($id);
		if ($data == null) {
			$content = [
				'code' => 400,
				'msg' => "获取失败！"
			];
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
			return;
		}
		$content = [
			'code' => 0,
			'msg' => "获取成功！",
			'data' => [
				$data
			]
		];
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		return;

	}

	/**
	 * 图片文件上传
	 */
	public function do_upload_image()
	{
		$config['upload_path'] = 'upimg/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_width'] = 0;
		$config['max_height'] = 0;
		$filename = time();
		$config['file_name'] = $filename;
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('file')) {
			$error = array('error' => $this->upload->display_errors());
			$content = [
				'code' => 400,
				'msg' => "上传失败",
				'data' => [
					'src' => ''
				]
			];
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		} else {
			$data = $this->upload->data();
			$file_name = $data['file_name'];
			$content = [
				'code' => 0,
				'msg' => "上传成功！",
				'data' => [
					'src' => '/upimg/' . $file_name
				]
			];
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		}
	}

	/**
	 * 视频文件上传
	 */
	public function do_upload_video()
	{
		$config['upload_path'] = './video/';
		$config['allowed_types'] = 'mp4';
		$config['max_width'] = 0;
		$config['max_height'] = 0;
		$config['max_size'] = 0;
		$this->load->model('server/news_model');
//		$filename = time();
//		$config['file_name'] = $filename;
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('file')) {
			$error = array('error' => $this->upload->display_errors());
			$content = [
				'errno' => 11,
				'data' => "请检查文件类型或大小！"
			];
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		} else {
			$data = $this->upload->data();
			$file_name = $data['file_name'];
			$url = '/video/' . $file_name;
			//查询文件是否存在
			if (!file_exists($url)) {
				$content = [
					'errno' => 11,
					'data' => "文件数据库已存在！"
				];
			}

			$content = [
				'errno' => 22,
				'data' => '写入数据库成功！'
			];

			$cont = [
				'table' => 'video',
				'data' => [
					'name' => $file_name,
					'url' => $url
				]
			];
			$isok = $this->news_model->insert_one($cont);
			if ($isok == 0) {
				exit(0);
			}
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		}

		//写入数据库，然后返回成功，前端在刷新页面
	}

	/**
	 * 图片文件上传 富文本
	 */
	public function do_upload_image2()
	{
		$config['upload_path'] = 'upimg/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_width'] = 1920;
		$config['max_height'] = 1080;
		$filename = time();
		$config['file_name'] = $filename;
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('file')) {
			$error = array('error' => $this->upload->display_errors());
			$content = [
				'errno' => 1,
				'data' => ["/upimg/1594010566.png"]
			];
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		} else {
			$data = $this->upload->data();
			$file_name = $data['file_name'];
			$content = [
				'errno' => 0,
				'data' => ['/upimg/' . $file_name]
			];
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		}
	}

	//查询操作
	public function select_news()
	{
		$this->load->model('server/news_model');
		$page = $_GET["page"];
		$limit = $_GET["limit"];
		$like = $_GET["like"]; //标题查询
		$category = $_GET["category"]; //文章分类查询

		if ($like == null || $like == "") {
			$like = null;
		} else {
			$like = ['title' => $like];
		}

		if ($category == null || $category == "") {
			$category = null;
		} else {
			$category = ['category' => $category];
		}
		$cont = [
			'table' => 'news', //要操作的表名
			'where' => $category,
			'like' => $like,
			'limit' => [($page - 1) * $limit, $limit], // 获取第一页 显示10行内容
			'order_by' => ['time' => 'DESC']
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组

		$i = 0;
		foreach ($data as $da) {
			//获取转义时间戳
			$da['time'] = date('Y-m-d', $da['time']);
			$data[$i]['time'] = $da['time'];

			//获取转义文件分类
			$classification_id=$da['category'];
			$cont = [
				'table'=>'classification',
				'where'=>[
					'id'=>$classification_id
				]
			];
			$arrs = $this->news_model->select_form($cont);  //返回数据的数组
			$data[$i]['category_name']=$arrs[0]['name'];
			$i++;
		}


		//print_r($data);
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}


	/**
	 * 写入文章内容
	 */
	public function write_news()
	{
		$title = $_POST["title"]; //post文章标题
		$title_url = $_POST["title_url"]; //post文章标题
		$introduction = $_POST["introduction"]; //post文章简介
		$time = $_POST["time"]; //post文章时间
		$content = $_POST["content"]; //post文章内容
		$category = $_POST["category"]; //post文章分类


		$this->load->model('server/news_model');
		$cont = [
			'table' => 'news',
			'data' => [
				'title' => $title,
				'title_url' => $title_url,
				'introduction' => $introduction,
				'time' => strtotime($time),
				'content' => $content,
				'category' => $category
			]
		];
		$isok = $this->news_model->insert_one($cont);
		$content = [
			'code' => 200,
			'msg' => "写入文章成功！"
		];
		if ($isok) {
			echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
			return;
		}
		$content['code'] = 400;
		$content['msg'] = "写入文章错误！";
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
		return;
	}

	/**
	 * 修改文章
	 */
	public function modify_news()
	{
		$id = $_POST["id"]; //post文章标题
		$title = $_POST["title"]; //post文章标题
		$title_url = $_POST["title_url"]; //post文章标题
		$introduction = $_POST["introduction"]; //post文章简介
		$time = $_POST["time"]; //post文章时间
		$content = $_POST["content"]; //post文章内容
		$category = $_POST["category"]; //post文章分类

		$this->load->model('server/news_model');
		$cont = [
			'table' => 'news',
			'data' => [
				'id' => $id,
				'title' => $title,
				'title_url' => $title_url,
				'introduction' => $introduction,
				'time' => strtotime($time),
				'content' => $content,
				'category' => $category
			]
		];
		$isok = $this->news_model->replace($cont);
		$content = [
			'code' => 200,
			'msg' => "修改文章成功！"
		];
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	/**
	 * 删除文章
	 */
	public function delete_news()
	{
		$id = $_GET["id"]; //post文章标题
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'news',
			'where' => [
				'id' => $id
			]
		];
		$isok = $this->news_model->delete($cont);
		$content = [
			'code' => 200,
			'msg' => "删除文章成功！"
		];
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	/**
	 * 获取视频数据库所有数据
	 */
	function select_video()
	{
		$page = $_GET["page"];
		$limit = $_GET["limit"];
		$this->load->model('server/news_model');
		//获取总数
		$cont = [
			'table' => 'video'
		];
		$count = $this->news_model->get_conunt($cont);

		$cont = [
			'table' => 'video', //要操作的表名
			'limit' => [($page - 1) * $limit, $limit]
		];
		$data = $this->news_model->select_all($cont);

		$content = [
			'code' => 200,
			'count' => $count,
			'data' => $data
		];
		echo json_encode($content, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	/**
	 * 删除视频 包括数据库以及文件夹
	 */
	function delete_video()
	{
		$file_id = $_GET["file_id"];
		$file_name = $_GET["file_name"];
		$file_url = $_GET["file_url"];
		$this->load->model('server/news_model');
		//删除数据库
		$cont = [
			'table' => 'video',
			'where' => [
				'id' => $file_id
			]
		];
		$isok = $this->news_model->delete($cont);

		$file_url = "./video/" . $file_name;
		//删除文件夹的文件
		if (!unlink($file_url)) {
			echo("删除错误");
		} else {
			echo("删除成功");
		}
	}

}
